home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / deluge / pluginmanagerbase.py < prev    next >
Text File  |  2009-06-16  |  6KB  |  171 lines

  1. #
  2. # pluginmanagerbase.py
  3. #
  4. # Copyright (C) 2007 Andrew Resch <andrewresch@gmail.com>
  5. #
  6. # Deluge is free software.
  7. #
  8. # You may redistribute it and/or modify it under the terms of the
  9. # GNU General Public License, as published by the Free Software
  10. # Foundation; either version 3 of the License, or (at your option)
  11. # any later version.
  12. #
  13. # deluge is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. # See the GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with deluge.    If not, write to:
  20. #     The Free Software Foundation, Inc.,
  21. #     51 Franklin Street, Fifth Floor
  22. #     Boston, MA  02110-1301, USA.
  23. #
  24. #    In addition, as a special exception, the copyright holders give
  25. #    permission to link the code of portions of this program with the OpenSSL
  26. #    library.
  27. #    You must obey the GNU General Public License in all respects for all of
  28. #    the code used other than OpenSSL. If you modify file(s) with this
  29. #    exception, you may extend this exception to your version of the file(s),
  30. #    but you are not obligated to do so. If you do not wish to do so, delete
  31. #    this exception statement from your version. If you delete this exception
  32. #    statement from all source files in the program, then also delete it here.
  33. #
  34.  
  35. #
  36.  
  37.  
  38. """PluginManagerBase"""
  39.  
  40. import os.path
  41.  
  42. import pkg_resources
  43.  
  44. import deluge.common
  45. import deluge.configmanager
  46. from deluge.log import LOG as log
  47.  
  48. METADATA_KEYS = [
  49.     "Name",
  50.     "License",
  51.     "Author",
  52.     "Home-page",
  53.     "Summary",
  54.     "Platform",
  55.     "Version",
  56.     "Author-email",
  57.     "Description",
  58. ]
  59.  
  60. class PluginManagerBase:
  61.     """PluginManagerBase is a base class for PluginManagers to inherit"""
  62.  
  63.     def __init__(self, config_file, entry_name):
  64.         log.debug("Plugin manager init..")
  65.  
  66.         self.config = deluge.configmanager.ConfigManager(config_file)
  67.  
  68.         # Create the plugins folder if it doesn't exist
  69.         if not os.path.exists(os.path.join(deluge.configmanager.get_config_dir(), "plugins")):
  70.             os.mkdir(os.path.join(deluge.configmanager.get_config_dir(), "plugins"))
  71.  
  72.         # This is the entry we want to load..
  73.         self.entry_name = entry_name
  74.  
  75.         # Loaded plugins
  76.         self.plugins = {}
  77.  
  78.         # Scan the plugin folders for plugins
  79.         self.scan_for_plugins()
  80.  
  81.     def enable_plugins(self):
  82.         # Load plugins that are enabled in the config.
  83.         for name in self.config["enabled_plugins"]:
  84.             self.enable_plugin(name)
  85.  
  86.     def disable_plugins(self):
  87.         # Disable all plugins that are enabled
  88.         for key in self.plugins.keys():
  89.             self.plugins[key].disable()
  90.  
  91.     def __getitem__(self, key):
  92.         return self.plugins[key]
  93.  
  94.     def get_available_plugins(self):
  95.         """Returns a list of the available plugins name"""
  96.         return self.available_plugins
  97.  
  98.     def get_enabled_plugins(self):
  99.         """Returns a list of enabled plugins"""
  100.         return self.plugins.keys()
  101.  
  102.     def scan_for_plugins(self):
  103.         """Scans for available plugins"""
  104.         plugin_dir = os.path.join(os.path.dirname(__file__), "plugins")
  105.         user_plugin_dir = os.path.join(deluge.configmanager.get_config_dir(), "plugins")
  106.  
  107.         pkg_resources.working_set.add_entry(plugin_dir)
  108.         pkg_resources.working_set.add_entry(user_plugin_dir)
  109.         self.pkg_env = pkg_resources.Environment([plugin_dir, user_plugin_dir])
  110.  
  111.         self.available_plugins = []
  112.         for name in self.pkg_env:
  113.             log.debug("Found plugin: %s %s",
  114.                 self.pkg_env[name][0].project_name,
  115.                 self.pkg_env[name][0].version)
  116.             self.available_plugins.append(self.pkg_env[name][0].project_name)
  117.  
  118.     def enable_plugin(self, plugin_name):
  119.         """Enables a plugin"""
  120.         if plugin_name not in self.available_plugins:
  121.             log.warning("Cannot enable non-existant plugin %s", plugin_name)
  122.             return
  123.  
  124.         plugin_name = plugin_name.replace(" ", "-")
  125.         egg = self.pkg_env[plugin_name][0]
  126.         egg.activate()
  127.         for name in egg.get_entry_map(self.entry_name):
  128.             entry_point = egg.get_entry_info(self.entry_name, name)
  129.             cls = entry_point.load()
  130.             instance = cls(self, plugin_name.replace("-", "_"))
  131.             instance.enable()
  132.             plugin_name = plugin_name.replace("-", " ")
  133.             self.plugins[plugin_name] = instance
  134.             if plugin_name not in self.config["enabled_plugins"]:
  135.                 log.debug("Adding %s to enabled_plugins list in config",
  136.                     plugin_name)
  137.                 self.config["enabled_plugins"].append(plugin_name)
  138.             log.info("Plugin %s enabled..", plugin_name)
  139.  
  140.     def disable_plugin(self, name):
  141.         """Disables a plugin"""
  142.         try:
  143.             self.plugins[name].disable()
  144.             del self.plugins[name]
  145.             self.config["enabled_plugins"].remove(name)
  146.         except KeyError:
  147.             log.warning("Plugin %s is not enabled..", name)
  148.  
  149.         log.info("Plugin %s disabled..", name)
  150.  
  151.     def get_plugin_info(self, name):
  152.         """Returns a dictionary of plugin info from the metadata"""
  153.         info = {}.fromkeys(METADATA_KEYS)
  154.         last_header = ""
  155.         cont_lines = []
  156.         for line in self.pkg_env[name][0].get_metadata("PKG-INFO").splitlines():
  157.             if not line:
  158.                 continue
  159.             if line[0] in ' \t' and (len(line.split(":", 1)) == 1 or line.split(":", 1)[0] not in info.keys()):
  160.                 # This is a continuation
  161.                 cont_lines.append(line.strip())
  162.             else:
  163.                 if cont_lines:
  164.                     info[last_header] = "\n".join(cont_lines).strip()
  165.                     cont_lines = []
  166.                 if line.split(":", 1)[0] in info.keys():
  167.                     last_header = line.split(":", 1)[0]
  168.                     info[last_header] = line.split(":", 1)[1].strip()
  169.  
  170.         return info
  171.